package me.sdevil507.biz.fruit.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.biz.fruit.dto.FruitDTO;
import me.sdevil507.biz.fruit.dto.FruitParamsDTO;
import me.sdevil507.biz.fruit.dto.ResultListFruitDTO;
import me.sdevil507.biz.fruit.mapper.FruitMapper;
import me.sdevil507.biz.fruit.po.FruitPO;
import me.sdevil507.biz.fruit.service.FruitImportExportService;
import me.sdevil507.biz.fruit.service.FruitService;
import me.sdevil507.supports.jpa.PageConvertCallBack;
import me.sdevil507.supports.jpa.PageResultDTO;
import me.sdevil507.supports.jpa.PageResultGenerator;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.status.ApiStatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @program: tips_back_end
 * @description: 成果展示类
 * @author: kzl
 * @create: 2020-06-23 16:39
 **/
@RestController
@RequestMapping("/api/plat/fruit")
@Slf4j
public class FruitPlatController {

    @Autowired
    private FruitService fruitService;

    @Autowired
    private FruitImportExportService fruitImportExportService;

    @Autowired
    private FruitMapper fruitMapper;

    @PostMapping("/v1/readAllByPage")
    public ApiResultDTO getAllByPage(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            Page<ResultListFruitDTO> pagePos = fruitService.readAllFruitByPage(fruitParamsDTO.getSearchDto(), fruitParamsDTO.getPageDto(), fruitParamsDTO.getSortDto());
            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pagePos);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }


    @PostMapping("/v1/readAll")
    public ApiResultDTO getAll(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            List<FruitDTO> list = fruitMapper.pos2dtos(fruitService.readAllOrgFruit(fruitParamsDTO.getSearchDto(), fruitParamsDTO.getSortDto()));
            apiResultDto = ApiResultGenerator.create("0", "获取信息列表成功!", list);
        } catch (Exception e) {
            log.error("获取信息列表失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "获取信息列表失败!");
        }
        return apiResultDto;
    }


}
